package wiki.xsx.jg.core;

import wiki.xsx.jg.annotation.*;
import java.lang.reflect.Field;

import java.io.File;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据表生成器
 */
public class TableGenerator {

    /**
     * 生成数据表
     * @param classNames 类名，多个逗号分隔
     * @param driverClassName 数据库驱动名称
     * @param dbUrl 数据库地址
     * @param userName 数据库用户名
     * @param password 数据库密码
     * @param rootPath 包的根路径
     * @param packageName 完整包名，如wiki.xsx.test
     * @throws Exception 异常信息
     */
    public static void run(
            String classNames,
            String driverClassName,
            String dbUrl,
            String userName,
            String password,
            String rootPath,
            String packageName
    ) throws Exception{
        DatabaseService service = DatabaseAdapter.getServiceInstance(
                driverClassName,
                dbUrl,
                userName,
                password,
                null,
                false
        );
        setEntityInfo(getClassNameList(classNames, rootPath, packageName), service.getDatabase());
        service.createTables(service.getDatabase().getTableSet());
    }

    /**
     * 根据类名称或包获取完整类名称集合
     * @param classNames 类名称，多个逗号分隔
     * @param rootPath 包的根路径
     * @param packageName 完整包名，如wiki.xsx.test
     * @return 返回完整类名集合
     */
    private static List<String> getClassNameList(String classNames, String rootPath, String packageName){
        List<String> list = new ArrayList<>();
        if (classNames!=null&&classNames.trim().length()>0){
            String names[] = classNames.split(",");
            for (String name : names){
                StringBuilder classNameBuilder = new StringBuilder();
                if (packageName!=null&&packageName.trim().length()>0){
                    classNameBuilder.append(packageName).append(".").append(name);
                }else{
                    classNameBuilder.append(name);
                }
                list.add(classNameBuilder.toString());
            }
        }else{
            StringBuilder pathBuilder = new StringBuilder(rootPath);
            if (packageName!=null&&packageName.trim().length()>0){
                String dirs[] = packageName.split("\\.");
                for (String dir : dirs){
                    pathBuilder.append(File.separator).append(dir);
                }
            }
            File fileDir = new File(pathBuilder.toString());
            if (fileDir.isDirectory()){
                String names[] = fileDir.list();
                for (String name : names){
                    if (name.endsWith(".java")){
                        StringBuilder classNameBuilder = new StringBuilder();
                        if (packageName!=null&&packageName.trim().length()>0){
                            classNameBuilder.append(packageName).append(".").append(name.substring(0, name.lastIndexOf(".java")));
                        }else{
                            classNameBuilder.append(name.substring(0, name.lastIndexOf(".java")));
                        }
                        list.add(classNameBuilder.toString());
                    }
                }
            }
        }
        return list;
    }

    /**
     * 根据完整类名称集合设置实体类相关信息
     * @param classNameList 完整类名称集合
     * @param database 数据库对象
     * @throws ClassNotFoundException 异常信息
     */
    private static void setEntityInfo(List<String> classNameList, Database database) throws ClassNotFoundException{
        for (String className : classNameList){
            Class<?> tempClass = Class.forName(className);
            if (tempClass.getAnnotation(Ignore.class)!=null){
                continue;
            }
            Comment classComment = tempClass.getAnnotation(Comment.class);
            Entity entity = new Entity(tempClass.getSimpleName().toLowerCase(), classComment!=null?classComment.value():null);
            while (tempClass!=null&&!tempClass.getName().equalsIgnoreCase("java.lang.object")){
                Field fields[] = tempClass.getDeclaredFields();
                for (Field field : fields){
                    Ignore ignore = field.getAnnotation(Ignore.class);
                    if (ignore==null){
                        wiki.xsx.jg.core.Field entityField = new wiki.xsx.jg.core.Field();
                        entityField.setName(field.getName());
                        entityField.setJavaType(field.getType());
                        Id id = field.getAnnotation(Id.class);
                        if (id!=null){
                            entityField.setIsPrimaryKey(true);
                        }
                        IsAutoIncrement isAutoIncrement = field.getAnnotation(IsAutoIncrement.class);
                        if (isAutoIncrement!=null){
                            entityField.setIsAutoIncrement(true);
                        }
                        IsNotNull isNotNull = field.getAnnotation(IsNotNull.class);
                        if (isNotNull!=null){
                            entityField.setIsNotNull(true);
                        }
                        DataType dataType = field.getAnnotation(DataType.class);
                        if (dataType!=null){
                            entityField.setType(dataType.value());
                        }
                        DefaultValue defaultValue = field.getAnnotation(DefaultValue.class);
                        if (defaultValue!=null){
                            if (
                                field.getType().isAssignableFrom(String.class)||
                                field.getType().isAssignableFrom(Date.class)||
                                field.getType().isAssignableFrom(Time.class)
                            ){
                                StringBuilder value = new StringBuilder();
                                value.append("'").append(defaultValue.value()).append("'");
                                entityField.setDefaultValue(value.toString());
                            }else{
                                entityField.setDefaultValue(defaultValue.value());
                            }
                        }
                        Comment fieldComment = field.getAnnotation(Comment.class);
                        if (fieldComment!=null){
                            entityField.setComment(fieldComment.value());
                        }
                        entity.getFieldMap().put(entityField.getName(), entityField);
                    }
                }
                tempClass = tempClass.getSuperclass();
            }
            database.getEntityMap().put(entity.getName(), entity);
        }
    }
}
